<template>
	<view class="face-box">
		<view class="face-tip">
			<text>请输入本人身份信息进行身份核验</text>
		</view>
		<xh-form-input title="姓名" placeholder="请输入姓名" v-model="name">
			<template #right>
				<image class="scan-icon" src="/static/scan_icon.png" mode="aspectFit" @tap="showSelectImage"></image>
			</template>
		</xh-form-input>
		<xh-form-input title="身份证号" placeholder="请输入身份证号" v-model="idCard"></xh-form-input>
		<view class="submit-btn face-btn" @tap="submitAction">
			身份核验
		</view>
		<xh-auth-type-pop ref="authTypeRef"></xh-auth-type-pop>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { getImageInfoByOcr, checkMerInfoByIdCard,checkFaceAuth } from '@/config/api.js'
	//姓名
	const name = ref('')
	//身份证号
	const idCard = ref('')
	//认证类型弹框ref
	const authTypeRef = ref()

	/* 选择照片*/
	const showSelectImage = async () => {
		try {
			const res = await uni.$tools.uploadImage()
			getOcrInfo(type, res)
		} catch (e) {
			uni.$tools.showToast(e)
		}
	}
	// 获取ocr识别信息
	const getOcrInfo = async (url) => {
		let params = {}
		params.fileUrl = url
		params.apiType = '01'
		params.frontOrBack = 'F'
		const res = await getImageInfoByOcr(params)
		const ocrInfo = JSON.parse(res.result.data)
		// 身份证正面信息
		const ocrRes = ocrInfo.words_result
		if (uni.$u.test.isEmpty(ocrRes)) {
			return
		}
		name.value = ocrRes['姓名'].words
		idCard.value = ocrRes['公民身份号码'].words
	}
	//提交
	const submitAction = () => {
		authTypeRef.value.open()
		return
		if (uni.$u.test.isEmpty(name.value)) {
			uni.$tools.showToast('请输入姓名')
			return
		} else if (uni.$u.test.isEmpty(idCard.value)) {
			uni.$tools.showToast('请输入身份证号')
			return
		}
		checkMerInfoByIdCard({ merPer: name.value, certNo: idCard.value }).then(res => {
			//0:不存在 1:存在 快录商户(商户未绑定) 2:快录存在(商户已绑定)/存在企业商户  3:存在小微商户
			const certStatus = res.result.certStatus
			const merId = res.result.merId
			const resMsg = res.result.resMsg
			if(certStatus == '1') {
				faceAuth(merId)
			}
			else if (certStatus == '2') {
				uni.$pop.show({
					type: 'alert',
					config: {
						title: '温馨提示',
						desc: resMsg,
						buttons: ['取消', '企业认证']
					}
				}, (value) => {
					if(value == '企业认证'){
						uni.navigateTo({
							url:'/pages/auth/enterprise-info/enterprise-info'
						})
					}
				})
			}
			else if (certStatus == '3') {
				uni.$pop.show({
					type: 'alert',
					config: {
						title: '温馨提示',
						desc: resMsg,
						buttons: ['取消', '小微认证']
					}
				}, (value) => {
					if(value == '小微认证'){
						uni.navigateTo({
							url:'/pages/auth/idcard-info/idcard-info'
						})
					}
				})
			}
			else if (certStatus == '4') {
				uni.$pop.show({
					type: 'face',
					config: {
						title: '温馨提示',
						desc: resMsg,
						buttons: ['重新登录', '取消']
					}
				}, (value) => {
					if(value == '重新登录'){
						uni.$auth.logout()
						uni.reLaunch({
							url:'/pages/login/login'
						})
					}
				})
			}
			else{
				//选择认证类型
				authTypeRef.value.open()
			}
		}).catch((err)=>{
			if(!uni.$u.test.isEmpty(err)){
				uni.$pop.show({
					type: 'face',
					config: {
						title: '温馨提示',
						desc: err.message.content,
						buttons: ['重新登录', '联系客服']
					}
				}, (value) => {
					if(value == '重新登录'){
						uni.$auth.logout()
						uni.reLaunch({
							url:'/pages/login/login'
						})
					}
				})
			}
		})
		
	}
	//人脸认证
	const faceAuth = (merId)=>{
		
		checkFaceAuth({ merPer: name.value, certNo: idCard.value }).then(res=>{
			//H5链接
			const htmlUrl = res.result.h5Url
			 //订单号
			const orderNo = res.result.orderNo
			uni.navigateTo({
				url:`/pages/auth/face-auth-web/face-auth-web?id=${merId}&code=${orderNo}&url=${htmlUrl}`
			})
		}).catch(err=>{
			
		})
	}
</script>

<style lang="scss" scoped>
	.face-tip {
		font-size: 28rpx;
		padding: 30rpx;
		color: $xh-subtitle-color;
	}

	.scan-icon {
		width: 28rpx;
		height: 28rpx;
		padding: 20rpx 0 20rpx 20rpx;
	}

	.face-btn {
		margin: 60rpx 75rpx 0;
	}
</style>